package com.windfone.web.action;

import java.io.BufferedReader;
import java.io.StringReader;

import org.springframework.jdbc.core.JdbcTemplate;

import com.opensymphony.xwork2.ActionSupport;

public class ExcuteSQLAction extends ActionSupport {

	private String sqlContent;
	public String getSqlContent() {
		return sqlContent;
	}
	public void setSqlContent(String sqlContent) {
		this.sqlContent = sqlContent;
	}
	private JdbcTemplate jdbcTemplate;
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	private void insertBaseData(){
		try {
			if ( sqlContent!=null){
				 BufferedReader read = new BufferedReader(  new StringReader(sqlContent));
				 String sql = "";
				 while(  (sql=read.readLine()) !=null){
					 String checksql = sql.toUpperCase();
					 if ( checksql.indexOf("DROP")!=-1 || checksql.indexOf("DELE")!=-1 ){
						 System.out.println("take error" );
					 } else {
						 jdbcTemplate.execute(sql);
					 }
				 }
			}
 		
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public String execute() throws Exception {
		 insertBaseData();
		 return SUCCESS;
	}

}
